import store from '@/store';
import Vue from 'vue';
import APP from '@/main'
export default {
	/**
	 * 权限控制
	 * @param {Array} arr 当前需要的权限数组
	 * @returns 
	 */
	auth(arr) {
		const access = store.state.userButtonAuth || [];
		let status = false;
		if (access.includes(arr) || arr == true) status = true;
		if (this.includeArray(access, arr)) status = true;
		return status;
	},
	/**
	 * @description 判断列表1中是否包含了列表2中的某一项
	 * 因为用户权限 access 为数组，includes 方法无法直接得出结论
	 * */
	includeArray(list1, list2) {
		let status = false;
		list2.forEach(item => {
			if (list1.includes(item)) status = true;
		});
		return status;
	},
	/**
	 * @param {String} url 跳转链接
	 * @param {String} isVerify 是否验证登录
	 * @param {String} hasIdentity 是否验证实名认证
	 * */
	goPage(url) {
		uni.navigateTo({
			url: url
		});
	},
	/**
	 * @param {String} url 跳转链接大于6换一种跳转方法
	 * */
	navigateTo: function(url) {
		if (getCurrentPages().length < 6) {
			uni.navigateTo({
				"animationType": "none",
				"animationDuration": 50,
				url: url
			});
		} else {
			uni.reLaunch({
				url: url
			});

		}
	},
	/**
	 * 深度克隆
	 * @param {*} obj 
	 * @returns 
	 */
	deepClone(obj) {
		return JSON.parse(JSON.stringify(obj))
	},
	/**
	 * 数组去重-对象
	 * @param {Array} arr
	 * @param {String} key
	 * @returns {Array}
	 */
	uniqueObj(arr, key) {
		var hash = []
		for (var i = 0; i < arr.length; i++) {
			for (var j = i + 1; j < arr.length; j++) {
				if (arr[i][key] === arr[j][key]) {
					++i
				}
			}
			hash.push(arr[i])
		}
		return hash
	},
	/**
	 * 删除数组重的某一个
	 * @param {Array} arr
	 * @param {Object | String} val
	 * @param {String} key  当val为object时key才有用
	 */
	removeByValue(arr, val, key = 'id') {
		for (var i = 0; i < arr.length; i++) {
			if (typeof val === 'object') {
				if (arr[i][key] == val[key]) {
					arr.splice(i, 1);
					break;
				}
			} else {
				if (arr[i] == val) {
					arr.splice(i, 1);
					break;
				}
			}
		}
	},
	/**
	 * 将时间解析为字符串
	 * @param {(Object|string|number)} time
	 * @param {string} cFormat
	 * @returns {string | null}
	 */
	parseTime(time, cFormat) {
		if (!time) return time
		if (arguments.length === 0) {
			return null
		}
		const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
		let date
		if (typeof time === 'object') {
			date = time
		} else {
			if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
				time = parseInt(time)
			}
			if (typeof time === "string" && time.indexOf("-") > -1) {
				time = time.replace(/-/g, '/');
			}
			if ((typeof time === 'number') && (time.toString().length === 10)) {
				time = time * 1000
			}
			date = new Date(time)
		}
		const formatObj = {
			y: date.getFullYear(),
			m: date.getMonth() + 1,
			d: date.getDate(),
			h: date.getHours(),
			i: date.getMinutes(),
			s: date.getSeconds(),
			a: date.getDay()
		}
		const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => {
			const value = formatObj[key]
			// Note: getDay() returns 0 on Sunday
			if (key === 'a') {
				return ['日', '一', '二', '三', '四', '五', '六'][value]
			}
			return value.toString().padStart(2, '0')
		})
		return timeStr // ios兼容
	},
	/**
	 * 数组格式转枚举对象格式
	 */
	getEnumObj(list, key = 'key', value = 'value') {
		var enums = {}
		for (let i = 0; i < list.length; i++) {
			const item = list[i];
			enums[item[key]] = item[value]
		}
		return enums
	},
	/** 获取url后面的参数
	 * @param {string} url
	 * @returns {Object}
	 */
	getQueryObject(url) {
		url = url == null ? window.location.href : url
		const search = url.substring(url.lastIndexOf('?') + 1)
		const obj = {}
		const reg = /([^?&=]+)=([^?&=]*)/g
		search.replace(reg, (rs, $1, $2) => {
			const name = decodeURIComponent($1)
			let val = decodeURIComponent($2)
			val = String(val)
			obj[name] = val
			return rs
		})
		return obj
	},
	/**
	 * @returns 获取当前页面路由及参数
	 */
	getCurPageParam() {
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		let curRoute = routes[routes.length - 1].route //获取当前页面路由
		let curParam = routes[routes.length - 1].options; //获取路由参数
		// 拼接参数
		let param = ''
		for (let key in curParam) {
			param += '&' + key + '=' + curParam[key]
		}
		let data = {}
		data.curRoute = curRoute
		data.curParam = curParam
		return data
	},
	/**
	 * 数字转整数 如 100000 转为10万
	 * @param {需要转化的数} num
	 * @param {需要保留的小数位数} point
	 */
	tranNumber(num, point = 2) {
		let numStr = parseInt(num).toString()

		// 1万以内直接返回
		if (numStr.length < 5) {
			return numStr;
		}
		//大于8位数是亿
		else if (numStr.length > 8) {
			let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + point);
			return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿';
		}
		//大于4位数是万 (以1W分割 1W以下全部显示)
		else if (numStr.length > 4) {
			let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
			return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万';
		}
	},
	// 拨打电话
	makePhoneCall(phoneNumber) {
		if (!phoneNumber) return
		uni.makePhoneCall({
			phoneNumber
		})
	},
	/**
	 * 复制文本
	 * @param {被复制的文本} text
	 * @param {复制成功的回调} callback
	 */
	copyText(text = '', callback = null) {
		// #ifdef H5
		const systemInfo = uni.getSystemInfoSync()
		if (systemInfo.platform === 'ios') {
			//此处放在setTimeout里面执行解决不能在async方法里面执行的问题，否则的话ios里面可能会复制不成功
			setTimeout(() => {
				APP.$copyText(text).then(
					(res) => {
						if (callback) {
							callback()
						}
					},
					(err) => {
						APP.$u.toast('复制失败')
					}
				)
			})
		} else {
			APP.$copyText(text).then(
				(res) => {
					if (callback) {
						callback()
					}
				},
				(err) => {
					APP.$u.toast('复制失败')
				}
			)
		}
		// #endif
		// #ifndef H5
		uni.setClipboardData({
			data: text,
			success: () => {
				if (callback) {
					callback()
				}
			}
		})
		// #endif
	}
}